﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyGrouping.UrlTree;
using DTO;

namespace MyGrouping
{
    public class WebpageGrouping
    {
        private static int seedRandom = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;

        /// <summary>
        /// Reconstruct the domain by building a directory tree from each page original URL.
        /// </summary>
        /// <param name="wpageLst">List of al webpages in this domain.</param>
        /// <param name="domainStr">The domain URL</param>
        /// <returns>Directory root node</returns>
        public static Node BuildUrlTree(List<Webpage> wpageLst, string domainStr)
        {
            Node root = new Node(domainStr);

            foreach (Webpage wp in wpageLst)
            {
                string temp = wp.URL;
                temp = temp.Replace(domainStr, string.Empty);
                string[] arrName = temp.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                Node curNode = root;
                Leaf leaf = new Leaf(arrName[arrName.Length - 1], wp.ID);

                for (int i = 0; i < arrName.Length - 1; i++)
                {
                    Node tempNode = curNode;
                    curNode = curNode.AddNode(arrName[i]);
                    curNode.Parent = tempNode;
                }

                curNode.AddLeaf(leaf);
                leaf.Parent = curNode;
            }

            RemoveDegenerateBranch(root);

            return root;
        }

        public static void RemoveDegenerateBranch(UrlNode node)
        {
            if (node.IsLeaf())
            {
                Leaf tempLeaf = (Leaf)node;
                Node tempParent = (Node)node.Parent;

                while (tempParent != null && tempParent.IsSingleDad())
                {
                    tempParent = (Node)tempParent.Parent;
                    tempParent.Children.Remove(tempLeaf.Parent);
                    tempLeaf.High++;
                    tempParent.Children.Insert(0, tempLeaf);
                    tempLeaf.Parent = tempParent;
                }
            }
            else
            {
                for (int i = 0; i < ((Node)node).Children.Count; i++)
                {
                    RemoveDegenerateBranch(((Node)node).Children[i]);
                }
            }
        }

        /// <summary>
        /// Categorize pages in this domain into groups.
        /// </summary>
        /// <param name="node">Domain root node</param>
        /// <returns>List of groups that contains webpage ID</returns>
        public static List<List<int>> Group(Node node)
        {
            List<int> group_1 = new List<int>();
            List<int> group_2 = new List<int>();
            List<List<int>> group_lst = new List<List<int>>();

            for (int i = 0; i < node.Children.Count; i++)
            {
                if (node.Children[i].IsLeaf() == true)
                {
                    Leaf tempLeaf = (Leaf)node.Children[i];
                    group_1.Add(tempLeaf.Id);
                    //if (tempLeaf.High == 0)
                    //{
                    //    group_1.Add(tempLeaf.Id);
                    //}
                    //else
                    //{
                    //    group_2.Add(tempLeaf.Id);
                    //}
                }
                else
                {
                    List<List<int>> tempGroupLst = WebpageGrouping.Group((Node)node.Children[i]);
                    group_lst.AddRange(tempGroupLst);
                }
            }

            if (group_1.Count > 1 || group_lst.Count == 0)
            {
                group_lst.Add(group_1);
            }
            else if (group_1.Count == 1)
            {
                group_lst[0].Add(group_1[0]);
            }

            //if (group_2.Count > 0)
            //{
            //    group_lst.Add(group_2);
            //}

            return group_lst;
        }

    }
}
